import {
	defineStore
} from 'pinia';
import helper from "../helper"
import router from "../router"
import user from "./user"
import commonApi from "@/sheep/api/common"
import isEmpty from 'lodash/isEmpty';
import cloneDeep from 'lodash/cloneDeep';
import clone from 'lodash/clone';
import { i18n } from "@/locale";

const app = defineStore({
	id: 'app',
	state: () => ({
		scss: {
			main: '#33D65E'
		},
		appName: '鲁班底盘',
		location: {
			lng: '104.131194',
			lat: '30.674045'
		},
		configInfo: {}, //所有配置设置
		language: uni.getStorageSync('language') || 'en',
		maxWidth:'1200rpx',
		brandList:[],  //品牌列表
		yewulists:[],  //业务列表
		jyproject:[],  //建议项目列表
		zzproject:[],   //增值服务列表
		
	}),

	actions: {
		switchLanguage(lang = "en") {
			this.language = lang;
			i18n.global.locale = lang;
			uni.setStorageSync('language', lang)
			uni.setLocale(lang);
		},
		//业务类型列表
		async getConfig(){
			const { code,data } = await commonApi.getconfig()
			if(code != 1) return
			this.configInfo = data
		},
		//品牌列表
		async getBrandList(){
			const { code,data } = await commonApi.brandslist()
			if(code != 1) return
			this.brandList = data
		},
		//业务类型列表
		async getYewulists(){
			const { code,data } = await commonApi.yewulists()
			if(code != 1) return
			this.yewulists = data
		},
		//建议项目列表
		async getJyproject(){
			const { code,data } = await commonApi.jyproject()
			if(code != 1) return
			this.jyproject = data
		},
		//增值服务列表
		async getZzproject(){
			const { code,data } = await commonApi.zzproject()
			if(code != 1) return
			this.zzproject = data
		},

		// #ifdef MP-WEIXIN 
		getSystemInfo() {
			let menuButtonInfo = uni.getMenuButtonBoundingClientRect();
			//获取胶囊对上高度
			this.searchBarTop = menuButtonInfo.top;
			//获取胶囊高度
			this.searchBarHeight = menuButtonInfo.height;
			return menuButtonInfo
		},
		// #endif
		getLocation() {
			let _this = this
			return new Promise((resolve, reject) => {
				uni.getSetting({
					success(res) {
						const statu = res.authSetting
						// #ifdef MP-WEIXIN || APP
						if (!statu['scope.userLocation']) {
							helper.showModal({
								content: '未同意位置消息，是否打开？'
							}).then(() => {
								uni.openSetting({
									success(data) {
										_this.getLocationTrue().then((res) => {
											resolve(res)
										}).catch(() => {
											reject(false)
										})
									}
								})
							}).catch(() => {
								reject(false)
							})
						} else {
							console.log('未打开手机系统的位置信息', statu);
							reject(false)
							helper.showModal({
								content: '您未打开手机GPRS定位',
								showCancel: false
							})
						}
						// #endif	
					},
					fail(err) {
						console.log(err, '设置打开失败');

					}
				})
			})
		},
		getLocationTrue(options = {
			refresh: false
		}) {
			let _this = this
			return new Promise(function(resolve, reject) {
				uni.getLocation({
					type: 'wgs84',
					success: function(res) {
						_this.location.lng = res.longitude
						_this.location.lat = res.latitude
						resolve({
							lng: res.longitude,
							lat: res.latitude
						})
					},
					fail(err) {
						_this.getLocation().catch(() => {
							resolve(false)
						}).then((res) => {
							if (!res) return resolve(false)
							_this.location.lng = res.lng
							_this.location.lat = res.lat
							resolve({
								lng: res.lng,
								lat: res.lat
							})
						})
						// reject()
					}
				});
			})

		}
	},
	persist: {
		enabled: true,
		strategies: [{
			key: 'app-store',
		}, ],
	},
});

export default app;